package com.skwen.delivery.superdelivery.order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skwen.delivery.superdelivery.BaseActivity;
import com.skwen.delivery.superdelivery.R;
import com.skwen.delivery.superdelivery.myGreenDao.manager.WriteOrderDBManager;
import com.skwen.delivery.superdelivery.order.adapter.WriteOrderAdapter;
import com.skwen.delivery.superdelivery.order.bean.WriteOrder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by skwen on 2017/11/10.
 * SuperDelivery 填写订单的确认详情并提示等待接单
 */

public class WriteOrderDetialActivity extends BaseActivity {


    @BindView(R.id.left_button)
    ImageButton mLeftButton;
    @BindView(R.id.title_name)
    TextView mTitleName;
    @BindView(R.id.right_text)
    TextView mRightText;
    @BindView(R.id.order_address)
    TextView mOrderAddress;
    @BindView(R.id.send_name)
    TextView mSendName;
    @BindView(R.id.send_phone)
    TextView mSendPhone;
    @BindView(R.id.goods_type)
    TextView mGoodsType;
    @BindView(R.id.goods_weight)
    TextView mGoodsWeight;
    @BindView(R.id.remark)
    TextView mRemark;
    @BindView(R.id.if_pay_after)
    TextView mIfPayAfter;
    @BindView(R.id.single_order_layout)
    LinearLayout mSingleOrderLayout;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.list_order_layout)
    LinearLayout mListOrderLayout;
    @BindView(R.id.order_no)
    TextView mOrderNo;
    @BindView(R.id.order_state)
    TextView mOrderState;

    private WriteOrderDBManager dbManager;
//    private WriteOrderAdapter mAdapter;
//    private List<WriteOrder> mList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_order_detial_layout);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        mTitleName.setText("订单详情");
        dbManager = WriteOrderDBManager.getInstance(this);
    }

    @OnClick(R.id.left_button)
    public void onViewClicked() {
        finish();
    }


    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        Observable.just("")
                .subscribeOn(Schedulers.newThread())
                .map(s -> dbManager.queryWriteOrderList())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<WriteOrder>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {
                    }

                    @Override
                    public void onNext(@NonNull List<WriteOrder> writeOrders) {
                        if (writeOrders.size() > 0) {
                            if (writeOrders.size() == 1) {
                                initSingle(writeOrders);
                            } else {
                                initList(writeOrders);
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable throwable) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }


    private void initSingle(List<WriteOrder> writeOrders) {
        mSingleOrderLayout.setVisibility(View.VISIBLE);
        mListOrderLayout.setVisibility(View.GONE);
        WriteOrder writeOrder = writeOrders.get(0);
        if (writeOrder != null) {
            mOrderNo.setText(getIfNullText(writeOrder.getId()+""));
            mOrderAddress.setText(getIfNullText(writeOrder.getReceiveAddress()));
            mSendName.setText(getIfNullText(writeOrder.getReceiveName()));
            mSendPhone.setText(getIfNullText(writeOrder.getReceicePhone()));
            mGoodsType.setText(getIfNullText(writeOrder.getGoodsType()));
            mGoodsWeight.setText(getIfNullText(writeOrder.getGoodsWeight()));
            mRemark.setText(getIfNullText(writeOrder.getGoodsRemark()));
            if (writeOrder.getIfReceivedPay() == 0) {
                mIfPayAfter.setText("在线支付");
            } else if (writeOrder.getIfReceivedPay() == 1) {
                mIfPayAfter.setText("货到付款");
            }
            mOrderState.setText("等待快递员确认...");
        }
    }

    private void initList(List<WriteOrder> writeOrders) {
        mSingleOrderLayout.setVisibility(View.GONE);
        mListOrderLayout.setVisibility(View.VISIBLE);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        WriteOrderAdapter mAdapter = new WriteOrderAdapter(writeOrders);
        mRecyclerView.setAdapter(mAdapter);
    }

}
